package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.LayoutTjhwBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.PagingViewHoder
import com.bumptech.glide.RequestManager

/**
 * 精选商品
 */
class TjGood2Adapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<GoodsIndex.TjGoods>(object : DiffUtil.ItemCallback<GoodsIndex.TjGoods>() {
        override fun areItemsTheSame(
            oldItem: GoodsIndex.TjGoods,
            newItem: GoodsIndex.TjGoods
        ): Boolean {
            return oldItem.goods_id == newItem.goods_id
        }

        override fun areContentsTheSame(
            oldItem: GoodsIndex.TjGoods,
            newItem: GoodsIndex.TjGoods
        ): Boolean {
            return oldItem == newItem
        }
    }) {

    override fun getItemLayout(): Int {
        return R.layout.layout_tjhw
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {
        val binding = holder.binding as LayoutTjhwBinding
        val bean = t as GoodsIndex.TjGoods
        binding.bean = bean
        manager.load(bean.goods_img).into(binding.img)

        TextTool.getBuilder("¥").setAlign(Layout.Alignment.ALIGN_CENTER).setBold().setTextSize(10)
            .append(bean.price.toString()).setForegroundColor(Color.parseColor("#E81837")).setBold().setTextSize(12)
            .into(binding.tvPrice)
        binding.tvPriceOld.apply {
            setPaintFlags(binding.tvPriceOld.getPaintFlags() or Paint.STRIKE_THRU_TEXT_FLAG)
        }

        var position = holder.layoutPosition

        if(position % 2==0){
            position=1
        }
//        LogUtils.i(position)
        binding.apply {
            when (position) {
                0 -> {
                    tvLabel.isVisible = true
                    tvLabel2.isVisible = false
                }
                1 -> {
                    tvLabel.isVisible = true
                    tvLabel2.isVisible = true
                }
                6 -> {
                    tvLabel.isVisible = false
                    tvLabel2.isVisible = false
                }
                else -> {
                    tvLabel.isVisible = true
                    tvLabel2.isVisible = false
                }
            }
            event = this@TjGood2Adapter
        }

    }

    fun itemClick(bean: GoodsIndex.TjGoods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

}